package br.com.leilao.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@NamedQueries({
		@NamedQuery(name = "getVencedorDoEdital", query = ""
				+ "SELECT l "
				+ "FROM Lance l where l.edital.idEdital = :idEdital"
				+ "		AND  1 in( "
				+ "				      select count(l2.valor) from Lance l2 where l2.valor =l.valor and l2.edital.idEdital = :idEdital)"
				+ "order by l.valor"),
		@NamedQuery(name = "getTodos", query = "SELECT i FROM Lance i " +
				"where i.usuario.idUsuario = :idUsuario") 
		})
@Entity
@Table(name = "lance")
public class Lance implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Integer idLance;
	private double valor;

	@ManyToOne(fetch = FetchType.LAZY)
	@Fetch(FetchMode.JOIN)
	@JoinColumn(referencedColumnName = "idUsuario")
	private Usuario usuario;

	@ManyToOne(fetch = FetchType.LAZY)
	private Edital edital;

	public Integer getIdLance() {
		return idLance;
	}

	public void setIdLance(Integer idLance) {
		this.idLance = idLance;
	}

	public double getValor() {
		return valor;
	}

	public void setValor(double valor) {
		this.valor = valor;
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public Edital getEdital() {
		return edital;
	}

	public void setEdital(Edital edital) {
		this.edital = edital;
	}

}